import { Row, Col,Input, Button, Form, Flex } from "antd";
import { FormInstance, useForm } from "antd/lib/form/Form";
import React from "react";
const { TextArea } = Input;
const rows = 0;
function b64EncodeUnicode(str:string) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        //@ts-ignore
        return String.fromCharCode('0x' + p1);
    }));
}
 
function b64DecodeUnicode(str:string) {
    return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}
    
const Base64Tool:React.FC = ()=>{

    const formRef = React.useRef<FormInstance>(null);
    const encode = ()=>{
       let input = formRef.current?.getFieldValue('input')
       formRef.current?.setFieldsValue({output:b64EncodeUnicode(input)})
 
    }
    const dncode = ()=>{
        let input = formRef.current?.getFieldValue('input')
        formRef.current?.setFieldsValue({output:b64DecodeUnicode(input)})
  
    }
    const swap = ()=>{
        let input = formRef.current?.getFieldValue('input')
        let output = formRef.current?.getFieldValue('output')
        formRef.current?.setFieldsValue({output:input,input:output})
    }
    const styels = {height:"60vh"}
    return <>
        <Form ref={formRef}>

       
        <Row gutter={10} >
            <Col span={10}>
                <Form.Item label="输入" name="input">
                <TextArea  style={styels} />
                </Form.Item>
            </Col>
            <Col span={4}>
                <Flex vertical={true}>
                    <Button onClick={encode} >编码</Button>
                    <Button onClick={dncode}>解码</Button>
                    <Button onClick={swap}>反转输入</Button>
                </Flex>
            </Col>
            <Col span={10}>
                <Form.Item label="输出" name="output">
                <TextArea  style={styels}/>
                </Form.Item>
            </Col>
            </Row>
        </Form>
    </>
}
export default Base64Tool;